package com.runda.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Properties;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.pobjects.fonts.FontManager;
import org.icepdf.core.util.GraphicsRenderingHints;

public class PdfUtil {
	public static final String FILETYPE_JPG = "jpg";
    public static final String SUFF_IMAGE = "." + FILETYPE_JPG;
    
    static{
    	String[] fontsPath={"/usr/share/fonts/winfonts"};
		FontManager.getInstance().readSystemFonts(fontsPath);
		Properties p=FontManager.getInstance().getFontProperties();
		System.out.println(p.elements());
    }
 
/**
     * 将指定pdf文件的首页转换为指定路径的缩略图
     *@param filepath 原文件路径，例如d:/test.pdf
     *@param imagepath 图片生成路径，例如 d:/test-1.jpg
     *@param zoom     缩略图显示倍数，1表示不缩放，0.3则缩小到30%
 * @throws IOException 
 * @throws PDFSecurityException 
 * @throws PDFException 
     */
    public static Document getPdfDocument(String filepath) throws PDFException, PDFSecurityException, IOException{
    	 Document document = new MyDocument();
         document.setFile(filepath);
         return document;
    }
    public static Document getPdfDocument(InputStream io,String fileName) throws PDFException, PDFSecurityException, IOException{
   	 	Document document = new MyDocument();
        document.setInputStream(io, fileName);
        return document;
   }
   public static String[] tranferAll(Document document, String imagepath,String fileNamePrefix, float zoom) throws PDFException, PDFSecurityException, IOException{
	   int maxPages = document.getPageTree().getNumberOfPages();
	   String [] images=new String[maxPages];   
	   for(int i=0;i<maxPages;i++){
		   images[i]=tranfer(document,imagepath,fileNamePrefix,zoom,i);
	   }
	   return images;
   }
   public static void tranfer(Document document, String imagepath,String fileNamePrefix, float zoom,int start,int end) throws PDFException, PDFSecurityException, IOException{
	   for(int i=start;i<end;i++){
		   tranfer(document,imagepath,fileNamePrefix,zoom,i);
	   }
   }
   public static String tranfer(Document document, String imagepath,String fileNamePrefix,float zoom,int pageNo)
            throws PDFException, PDFSecurityException, IOException {
        // ICEpdf document class
	   String fileName=fileNamePrefix+pageNo+SUFF_IMAGE;
	   String outPath=imagepath+"/"+fileName;
        float rotation = 0f;
        // maxPages = document.getPageTree().getNumberOfPages();
        BufferedImage img = (BufferedImage) document.getPageImage(pageNo,
                GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation,
                zoom);
 
        Iterator<ImageWriter> iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);
        
        File outFile = new File(outPath);	
        write2Image(iter.next(),outFile,img) ;
        return fileName;
    }
    
    public static void write2Image(ImageWriter writer,File outFile,BufferedImage img) throws IOException{
         
         FileOutputStream out = new FileOutputStream(outFile);
         ImageOutputStream outImage = ImageIO.createImageOutputStream(out);
         writer.setOutput(outImage);
         writer.write(new IIOImage(img, null, null));
    }
    public static void main(String args[]){
    	try {
			//PdfUtil.tranfer("/Users/zhujiang/Downloads/51lietou-某先生(007422062).pdf", "/Users/zhujiang/Desktop/tempfile/", 1.5f);
    		Document document=PdfUtil.getPdfDocument("G://aaaa.pdf");
    		PdfUtil.tranferAll(document, "G://", "z_", 1.5f);
    		
    	} catch (PDFException e) {
			e.printStackTrace();
		} catch (PDFSecurityException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
}
